//
//  ChooseConditionCell.m
//  CarUser
//
//  Created by 王森林 on 2018/12/19.
//  Copyright © 2018年 王森林. All rights reserved.
//

#import "ChooseConditionCell.h"

#define BTNTAG 100

@interface ChooseConditionCell()

@property (nonatomic, strong) NSString *titleName;
@property  (nonatomic, strong)  NSArray  *btnAry;
@property  (nonatomic, copy) void (^resultBlock)(id  response);

@property (nonatomic, strong) UILabel *lineLabel;
@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic) NSInteger currentIndex;
@property (nonatomic) NSInteger preIndex;
@end

@implementation ChooseConditionCell


- (void)seTitleWithName:(NSString *)title withAry:(NSArray *)dataAry withBlock:(void (^)(id _Nonnull))result
{
    self.titleName =  title;
    self.btnAry = [NSArray arrayWithArray:dataAry];
    self.resultBlock = result;
    [self createUIWithData];
    self.currentIndex = -1;
    self.preIndex = -1;
    
    
}

- (void)createUIWithData
{
    [self.contentView  removeAllSubviews];
    [self.contentView addSubview:self.lineLabel];
    [self.contentView addSubview:self.titleLabel];
    [self createConstraint];
    self.titleLabel.text = self.titleName;
    
    __block float width = (BOUNDWIDTH - 15*2-8*3)/4;
    __block  float height = 30.0f;
    CWeakSelf;
    [self.btnAry enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *name = @"";
        if ([obj isKindOfClass:[NSString class]]) {
            name = obj;
        } else {
            name = [obj objectForKey:@"name"];
            
        }
        
        UIButton *btn = [self createBtnWithTitle:name];
//        btn.frame  = CGRectMake(15+(width+8)*(idx%4), 40+(height+10)*(idx/4), width, height);
        [weakSelf.contentView addSubview:btn];
        btn.tag = BTNTAG + idx;
        [btn addTarget:self action:@selector(handleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(weakSelf.contentView.mas_left).offset(15+(width+8)*(idx%4));
            make.top.mas_equalTo(weakSelf.contentView.mas_top).offset(40+(height+10)*(idx/4));
            make.height.mas_equalTo(height);
            make.width.mas_equalTo(width);
        
        }];
        
        
        if (weakSelf.btnAry.count - 1 == idx) {
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                 make.bottom.equalTo(weakSelf.contentView).offset(-10);
            }];
           
        }
        
    }];
    
    
}

-(void)createConstraint
{
    
    [self.lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).offset(15);
        make.left.equalTo(self.contentView.mas_left).offset(15);
        make.width.mas_equalTo(4);
        make.height.mas_equalTo(15);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).offset(15);
        make.left.equalTo(self.lineLabel.mas_right).offset(4);
        make.width.mas_equalTo(150);
        make.height.mas_equalTo(15);
    }];
    
}


- (void)handleBtnAction:(id)sender
{
    UIButton *btn = sender;
    [self updateBtnStatus:btn.tag-BTNTAG];
    
}

- (void)updateBtnStatus:(NSInteger)index
{
    
    UIButton  *currentBtn = (UIButton *)[self.contentView viewWithTag:index+BTNTAG];
    id obj = self.btnAry[currentBtn.tag - BTNTAG];
    NSString *name = @"";
    if ([obj isKindOfClass:[NSString class]]) {
        name = obj;
    } else {
        name = [obj objectForKey:@"name"];
        
    }
    
    NSMutableDictionary *result = [[NSMutableDictionary alloc] init];
    [result setObject:name forKey:@"title"];
    [result setObject:[NSString stringWithFormat:@"%ld",index] forKey:@"index"];
    if (!currentBtn.selected) {
        [result setObject:@"1" forKey:@"operation"];
        currentBtn.selected = YES;
        currentBtn.layer.borderColor = RGB(40, 40, 40).CGColor;
    } else {
        [result setObject:@"2" forKey:@"operation"];
        currentBtn.selected = NO;
        currentBtn.layer.borderColor = Common_TextLowColor.CGColor;
    }
   
    if ([self.titleName isEqualToString:@"车身"]) {
        [result setObject:@"1" forKey:@"section"];
    }
    if ([self.titleName isEqualToString:@"座数"]) {
        [result setObject:@"2" forKey:@"section"];
    }
    if ([self.titleName isEqualToString:@"预算"]) {
        [result setObject:@"3" forKey:@"section"];
    }
    
    if (self.resultBlock) {
        self.resultBlock(result);
    }

    self.preIndex = self.currentIndex;
    self.currentIndex = index;
    if (self.preIndex != -1) {
        if (self.preIndex == index) {
            return;
        }
        UIButton *preBtn = (UIButton *)[self.contentView viewWithTag:self.preIndex+BTNTAG];
        preBtn.selected = NO;
        preBtn.layer.borderColor = Common_TextLowColor.CGColor;
//        NSString *title = self.btnAry[self.preIndex];
        id obj = self.btnAry[self.preIndex];
        NSString *name = @"";
        if ([obj isKindOfClass:[NSString class]]) {
            name = obj;
        } else {
            name = [obj objectForKey:@"name"];
            
        }
        if ([self.titleName isEqualToString:@"车身"]) {
            [result setObject:@"1" forKey:@"section"];
        }
        if ([self.titleName isEqualToString:@"座数"]) {
            [result setObject:@"2" forKey:@"section"];
        }
        if ([self.titleName isEqualToString:@"预算"]) {
            [result setObject:@"3" forKey:@"section"];
        }
        [result setObject:name forKey:@"title"];
        [result setObject:@"2" forKey:@"operation"];
        [result setObject:[NSString stringWithFormat:@"%ld",index] forKey:@"index"];
        
        if (self.resultBlock) {
            self.resultBlock(result);
        }
    }
   
    
 
    

    
  
 
    
}


- (UIButton *)createBtnWithTitle:(NSString  *)title
{
    UIButton  *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:Common_TextLowColor forState:UIControlStateNormal];
    [btn setTitleColor:RGB(40, 40, 40) forState:UIControlStateSelected];
    btn.titleLabel.font = FONT_SIZE(12);
    btn.layer.cornerRadius = 5;
    btn.layer.masksToBounds = YES;
    btn.layer.borderColor =  Common_TextLowColor.CGColor;
    btn.layer.borderWidth = 1;
    return btn;
    
    
}


- (UILabel *)lineLabel
{
    if (!_lineLabel) {
        _lineLabel = [UILabel new];
        _lineLabel.backgroundColor = Common_TextLowColor;
        _lineLabel.layer.cornerRadius = 3;
        _lineLabel.layer.masksToBounds = YES;
        
    }
    return _lineLabel;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textColor = [UIColor blackColor];
        _titleLabel.font = FONT_SIZE(16);
        
    }
    return _titleLabel;
    
}

@end
